package tech.aister.day01.homework;

import java.sql.SQLOutput;
import java.util.ArrayList;
import java.util.Arrays;

public class Employee {
    private int id;
    private String name;
    private double salary;

    private Employee[] employees;

    public Employee(int id,String name,double salary){
        this.id = id;
        this.name = name;
        this.salary = salary;
    }

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    public double getSalary() {
        return salary;
    }
    public void setSalary(double salary) {
        this.salary = salary;
    }

    public Employee[] getEmployees() {
        return employees;
    }

    public void setEmployees(Employee[] employees) {
        this.employees = employees;
    }

    @Override
    public String toString() {
        return "Employee{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", salary=" + salary +
                '}';
    }

    public void EmpAdd(Employee employee){
        if(null==employee){
            System.out.println("sorry,您没有权限添加!");
            return;
        }

        employees = Arrays.copyOf(employees,employees.length+1);
        employees[employees.length-1]=employee;
    }

    public void findAll(){
        if(null!=employees && employees.length>0){
            for(Employee employee : employees){
                System.out.println(employee);
            }
        }
        else{
            System.out.println("sorry,请先添加下属!");
        }
    }

    public void delById(int id){

        if(null!=employees && employees.length>0){
            int count = 0;
            for (int i = 0; i < employees.length; i++) {
                if(employees[i].getId()==id){
                    count++;

                    break;
//                    employees[i] = employees[employees.length-1];
//                    employees = Arrays.copyOf(employees,employees.length-1);
                }
            }
            if(count==0){
                System.out.println("sorry,没有找到相关下属!");
                return;//直接结束整个方法...
            }
            Employee[] emps = new Employee[employees.length-1];//因为id是唯一的.只能一次只能删除1个

            //下标计数器
            int pos = 0;
            //需要遍历原来的数组
            for (Employee employee : employees) {
                if(employee.getId()!=id){
                    //数组的下标必须是连续的
                    emps[pos++] = employee;
                }
            }
            //System.out.println(emps.length);
            //employees => 指向的还是原来的数组
            //emps才是指向不包含id记录的新的数组
            employees = emps;

        }else{
            System.out.println("sorry,没有任何下属信息!");
        }
//            for(Employee employee : employees){
//                System.out.println(employee);
//            }

    }

    public void delByPrice(double prise) {

        if(null!=employees && employees.length>0){
            for (int i = 0; i < employees.length; i++) {
                if(employees[i].getSalary()>=prise){
                    employees[i] = employees[employees.length-1];
                    employees = Arrays.copyOf(employees,employees.length-1);
                }
            }
            for(Employee employee : employees){
                System.out.println(employee);
            }
        }
    }
}
